import router from './router'
import store from './store'
router.beforeEach((to, from, next) => {
  const token = store.getters.token // token
  const url = to.path // 目标路径
  const whiteList = ['/login', '/404']
  // 有token=>登录页
  if (token && url === '/login') {
    next('/')
  }
  // 有==>非登陆页
  if (token && url !== '/login') {
    next()
  }
  // 无token=>白名单
  if (!token && whiteList.includes(url)) {
    next()
  }
  // 无token 非白名单=>登录页
  if (!token && !whiteList.includes(url)) {
    // 保留重定向
    next(`/login?redirect=${url}`)
  }
})
